/**
 *  auth: Wayne
 *  function: 轮播图接口
 * */

let express = require('express');
let router = express.Router();
let slide = require("../model/slide");
let util=require("../util/util");


/**
 * @function 新增vps
 * @author wayne
 * @date 2019-02-19
 * @param
 */
router.post('/add',function (req,res) {
    const newObj = new slide(req.body);
    let name = req.body.name;
    let findparams={
        name:name
    }
    if(name){
        slide.find(findparams, (err,results)=> {
            if(err){
                res.send(util.reObj(-1,'添加失败',err))
            }else {
                if(results.length>0){
                    res.send(util.reObj(-2,'exist'))
                }else{
                    newObj.save( (err, result)=>{
                        if(err){
                            res.send(util.reObj(-1,'添加失败'))
                        }else{
                            res.send(util.reObj(0,'添加成功',result))
                        }
                    })
                }
            }
        })
    }else{
        res.send(util.reObj(-1,'轮播图名称不能为空'))
    }


})
/**
 * @function 删除slide
 * @author wayne
 * @date 2019-02-19
 * @param
 */
router.post('/del',function (req,res) {
    let slideid = req.body.slideid,
        params={
            _id:slideid
        };
    if(slideid) {
        slide.remove(params,(err,result)=>{
            if (err) {
                res.send(util.reObj(-1,'del error'))
            }
            else {
                res.send(util.reObj(0,'success'))
            }
        })

    }else{
        res.send(util.reObj(-1,'slideid can not be null'))
    }

})
/**
 * @function 查询slide
 * @author wayne
 * @date 2019-02-19
 * @param
 */
router.get('/find',async (req,res)=>{
    let param={},
        skipnum=req.query.skipnum||0,
        pagesize=req.query.pagesize||10;

    const reg = new RegExp(req.query.name, 'i') //不区分大小写
    if(req.query.name)
    {
        param.name={$regex : reg}
    }

    try {
        const results = await slide.find(param,null,{skip: parseInt(skipnum), limit:parseInt(pagesize),sort: {register_time: -1}})
        const count =await slide.count(param,null,{skip: parseInt(skipnum), limit:parseInt(pagesize),sort: {register_time: -1}})
        res.send(util.reObj("0",'success',results,count))
    }
    catch (err) {
        res.send(util.reObj(-1,'failed',err))
    }



})
/**
 * @function 修改slide
 * @author wayne
 * @date 2019-02-19
 * @param
 */
router.post('/update',function (req,res) {
    let _id = req.body.slideid,
        name = req.body.name,
        comment = req.body.comment||'',
        imageurl = req.body.imageurl||'',
        pageurl = req.body.pageurl||'';

    let findparams={
        _id:_id
    }
    let updateparams={
        name:name,
        comment:comment,
        imageurl:imageurl,
        pageurl:pageurl
    }
    for (var key of Object.keys(updateparams)) {
        if(!updateparams[key])
            delete updateparams[key]
    }
    if(_id){
        slide.findOneAndUpdate(findparams,{$set:req.body},function (err,result) {
            if(err){
                res.send(util.reObj(-1,'failed',err))
            }else{
                res.send(util.reObj(0,'success',result))
            }
        })
    }else{
        res.send(util.reObj(-1,'slideid can not be null,undefined,empty'))
    }
})


module.exports = router;
